<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>动态表格</title>
    <style>
        table {
            width: 500px;
            margin: 100 auto;
            border-collapse: collapse;
            text-align: center;
        }
        
        td,
        th {
            border: 1px solid #333;
        }
        
        thead tr {
            height: 40px;
            border-color: #ccc;
        }
    </style>
</head>

<body>
    <table>
        <thead>
            <th>姓名</th>
            <th>科目</th>
            <th>成绩</th>
            <th>操作</th>
        </thead>
        <tbody>

        </tbody>
    </table>
    <script>
        //准备学生数据,实现动态渲染
        var datas = [{
            name: 'wzc',
            subject: 'JavaScript',
            score: 100
        }, {
            name: 'wb',
            subject: 'JavaScript',
            score: 99
        }, {
            name: 'xh',
            subject: 'JavaScript',
            score: 98
        }, {
            name: 'aw',
            subject: 'JavaScript',
            score: 97
        }, {
            name: 'dzm',
            subject: 'JavaScript',
            score: 96
        }];

        //2.为tbody创建行  通过数组长度创建行数
        var tbody = document.querySelector('tbody');
        for (var i = 0; i < datas.length; i++) {
            //创建tr
            var tr = document.createElement('tr');
            tbody.appendChild(tr);

            //行里面创建单元格 数量取决于对象属性个数
            for (var k in datas[i]) {
                //创建单元格
                var td = document.createElement('td');
                //把对象里面的属性值给td
                td.innerHTML = datas[i][k];
                tr.appendChild(td);
            }
            //创建删除操作的单元格
            var td = document.createElement('td');
            td.innerHTML = '<a href="javascript:;">删除</a>';
            tr.appendChild(td);
        }
        //4.删除操作
        var as = document.querySelectorAll('a');
        for (var i = 0; i < as.length; i++) {
            as[i].onclick = function() {
                //点击删除a标签 删除所在行
                tbody.removeChild(this.parentNode.parentNode)
            }
        }
    </script>
</body>

</html>